package com.hhs.storage.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.hhs.storage.bean.*;
import com.hhs.storage.service.LeaderTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  领导派发任务列表
 * </p>
 *
 * @author hhs
 * @since 2023-05-12
 */
@RestController
@RequestMapping("/leaderTask")
@ApiSupport(order = 1)
@Api(tags = "领导交待")
public class LeaderTaskController {
    @Resource
    private LeaderTaskService leaderTaskService;

    /**
     * 获取表格数据
     * @param leaderTask
     * @param inputParam  relateType 指定类型数据  leader=领导  user=下属
     * @return
     */
    @RequestMapping(value = "table")
    @ApiOperation(value = "获取表格数据",
            httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(dataTypeClass = String.class,name = "page", value = "页码",required =
                    true,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "size", value = "每页条数",required =
                    true,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = Integer.class,name = "readFlag", value = "是否已读('':全部 0:未读 1:已读)",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "relateType", value = "关系类型",required =
                    true,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "leaderTaskStartDate", value = "起始时间",required =
                    false,paramType = "query"),
            @ApiImplicitParam(dataTypeClass = String.class,name = "leaderTaskEndDate", value = "结束时间",required =
                    false,paramType = "query"),
    })
    @ApiOperationSupport(order = 1)
    public ResponseBeanPage<LeaderTask> getAll(@ApiIgnore LeaderTask leaderTask, @ApiIgnore InputParam inputParam) {
        Map<String,Object> map=leaderTaskService.getAll(leaderTask,inputParam);
        ResponseBeanPage rbp = ResponseBeanPage.getInstance();

//        Map<String,Object> data=new HashMap<>();
//        int notcomplete=0;
//        int complete=0;
//        for (LeaderTask t :
//                rows) {
//            if(0==t.getStatus()){
//                notcomplete++;
//            }
//            if(1==t.getStatus()){
//                complete++;
//            }
//        }
//        data.put("notcomplete",notcomplete);
//        data.put("complete",complete);
        List<LeaderTask> rows = (List<LeaderTask>) map.get("rows");
        rbp.setCode(1);
        rbp.setData((Map)map.get("data"));
        rbp.setRows(rows);
        rbp.setTotal((Long) map.get("total"));
        return rbp;
    }

    /**
     * 新增编辑
     * @param leaderTask 任务内容
     * @param leaderTask 关联用户id
     * @return
     */
    @PostMapping(value = "edit")
    public Map<String, Object> addOrUpdate(@RequestBody LeaderTask leaderTask){
        return leaderTaskService.addOrUpdate(leaderTask,leaderTask.getMemberIds(),leaderTask.getType());
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping(value = "dele")
    public Map<String, Object> dele(String id) {
        try{
            return leaderTaskService.dele(id);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseInfo.error("存在关联业务，无法删除");
        }

    }

    /**
     * 修改状态
     * @param id
     * @param status
     * @return
     */
    @PostMapping(value = "stateChange")
    public Map<String, Object> stateChange(Integer id,Integer status) {
        return leaderTaskService.stateChange(id,status);

    }
    /**
     * 详情
     * @param id
     * @return
     */
    @PostMapping(value = "detail")
    public Map<String, Object> detail(@RequestParam Integer id) {
        return leaderTaskService.detail(id);

    }
    /**
     * 获取任务相关人员信息
     * @param type
     * @return
     */
    @PostMapping(value = "relatePersonalList")
    public Map<String, Object> relatePersonalList(@RequestParam String type) {
        return leaderTaskService.relatePersonalList(type);

    }

    @PostMapping(value = "remind")
    public Map<String, Object> remind( ) {
        return leaderTaskService.remind();

    }
}

